// pages/search/search.js
var httpUtil = require('../../../../utils/httpUtil.js');
var uiUtil = require('../../../../utils/uiUtil.js');
const app = getApp();
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchHistory:[], //历史搜索记录
    userLocation: null,//用户地理位置

    searchLists:{ //搜索结果
      totalCount: null,
      pageCount: null,
      pageNo: "1",
      pageSize: "1000",
      dataList: []
    },
    delIcon:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.setData({
      userLocation: app.globalData.userLocation
    });
    that.getSearchHistory();
  },

  /**
   * 搜索
   */
  searchMch: function (res, text){
    let searchValue =text|| res.detail.value.search
    let pageNo = that.data.searchLists.pageNo;
    let pageSize = that.data.searchLists.pageSize;
    let lat = that.data.userLocation.pois[0].location.lat;
    let lng = that.data.userLocation.pois[0].location.lng;
   
    if (searchValue==""){
      uiUtil.showToast("请输入搜索内容");
    } else {

      var requestTask = httpUtil.PostRequest("searchMch", {
        mchName: searchValue,
        pageNo: pageNo,
        pageSize: pageSize,
        lat: lat,
        lng: lng
      }, {
        success(res) {
          if (res.dataList==0){
            uiUtil.showToast("未搜索到相关内容");
          } else {
            that.setData({
              searchLists: res
            })
            that.saveSearchHistory([searchValue]);
          }
        },
        fail(code, res) {
          console.log(code, res)
          uiUtil.showToast(res);
        },
        complete() { }
      });
    }
  },

  /**
   * 历史记录点击
   */
  searchHistory(e){
    let text = e.currentTarget.dataset.text;
    that.setData({
      value: text
    })
    that.searchMch("",text);
  },

  /**
   * 历史记录删除显示隐藏
   */
  getDelIcon:function(){
    that.setData({
      delIcon: !that.data.delIcon
    })
  },

  /**
   * 历史记录查询
   */
  getSearchHistory:function(){
    try {
      var searchHistory = wx.getStorageSync('searchHistory');
      if (searchHistory) {
        that.setData({
          searchHistory: searchHistory
        })
      }
      if (searchHistory.length==0){
        that.setData({
          delIcon: false
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 历史记录保存
   */
  saveSearchHistory(e){
    var searchHistory;
    var index=that.data.searchHistory.indexOf(e[0]);
    if (index > -1) {
      that.data.searchHistory.splice(index, 1);
    }
    searchHistory = e.concat(that.data.searchHistory);

    try {
      wx.setStorageSync('searchHistory', searchHistory);
      that.getSearchHistory();
    } catch (e) { }
  },

  /**
   * 历史记录删除
   */
  delSearchHistory:function(e){
    wx.showModal({
      title: '清除历史记录',
      success(res) {
        if (res.confirm) {
          wx.setStorageSync('searchHistory', []);
          that.getSearchHistory();
        } else if (res.cancel) {
        }
      }
    })
    // let text = e.currentTarget.dataset.text;
    // let index = e.currentTarget.dataset.index;

    // let searchHistory = that.data.searchHistory;
    // searchHistory.splice(index, 1);
    
    // try {
    //   wx.setStorageSync('searchHistory', searchHistory);
    //   that.getSearchHistory();
    // } catch (e) { }
  }

})